package com.ylc.service.impl;

import com.ylc.domain.Accounts;
import com.ylc.mapper.UsersMapper;
import com.ylc.domain.Users;
import com.ylc.service.AccountsService;
import com.ylc.service.UsersService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.InputStream;

/**
 * @Author: ylc
 * @Description:
 * @Date Created in 2022-07-20 13:56
 */
@Service
public class UsersServiceImpl implements UsersService {
/*    //1.读取核心配置文件
    InputStream in = Resources.getResourceAsStream("SqlMapConfig.xml");
    //2.创建工厂对象
    SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(in);
    //3.取出sqlSession
    sqlSession = factory.openSession(true);//自动提交事务
    //4.取出动态代理的对象,完成接口中方法的调用,实则是调用xml文件中相的标签的功能
    usersMapper = sqlSession.getMapper(UsersMapper.class);*/
    @Autowired
    UsersMapper usersMapper;

    @Autowired
    AccountsService accountsService;

    @Override
    public int insert(Users users) {
        int num = 0;
        num = usersMapper.insert(users);
        System.out.println("用户添加成功！num=" + num);
        // 调用accountsService.save()
        num = accountsService.save(new Accounts(2,"redleaf","admin"));
        return num;
    }
}
